package com.fenye.testflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.dto.SysUserDao;
import com.fenye.testflow.model.entity.system.BaseEntity;
import com.fenye.testflow.model.entity.system.SysUser;
import com.fenye.testflow.model.entity.system.SysUserRole;
import com.fenye.testflow.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/user")
public class SysUserController{
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/save")
    public Result save(@RequestBody SysUser user) {
        System.out.println("user =="+user.toString());
        sysUserService.save(user);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/update")
    public Result edit(@RequestBody SysUser user) {
        sysUserService.updateById(user);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping( "/deleteById/{userId}")
    public Result deleteById(@PathVariable Long userId) {
        sysUserService.removeById(userId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/getById/{userId}")
    public Result<SysUser> getById(@PathVariable Long userId) {
        return Result.build(sysUserService.getById(userId), ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<SysUser> findByPage(SysUserDao sysUserDao ,
                             @PathVariable("pageNum") Long pageNum,
                             @PathVariable("pageSize") Long pageSize){
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysUser> queryWarpper = new LambdaQueryWrapper<>();
        if (sysUserDao.getKeyword() !=null && sysUserDao.getKeyword().trim().length()>0) {
            queryWarpper.like(SysUser::getUsername,sysUserDao.getKeyword());
        }
        if (sysUserDao.getCreateTimeBegin() !=null && sysUserDao.getCreateTimeBegin().trim().length()>0 && sysUserDao.getCreateTimeBegin() !=null && sysUserDao.getCreateTimeBegin().trim().length()>0) {
            queryWarpper.between(BaseEntity::getCreateTime,sysUserDao.getCreateTimeBegin(),sysUserDao.getCreateTimeEnd());
        }

        Page<SysUser> pageData = sysUserService.page(page, queryWarpper);
        return Result.build(pageData,ResultCodeEnum.SUCCESS);
    }


    /**
     * 为用户添加角色
     */


    @PostMapping("saveSysUserRole")
    public Result saveUserRole(@RequestBody SysUserRole sysUserRole){
      Boolean isSave = sysUserService.saveUserRole(sysUserRole);
      return Result.build(isSave,ResultCodeEnum.SUCCESS);
    }


    /**
     * 根据角色查询用户
     */
    @GetMapping("getUserForRoles/{roleCode}")
    public Result<SysUser> getUserForRoles(@PathVariable String roleCode){
      List<SysUser> userForRoleList = sysUserService.getUserForRoleList(roleCode);
      return Result.build(userForRoleList,ResultCodeEnum.SUCCESS);
    }


}
